﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;
using System.Windows.Forms;

namespace Common
{
    public class WriteLog
    {


        static object Locker = new object();

        static ExeConfigurationFileMap map = new ExeConfigurationFileMap();//读配置文件的

        public static void WriteLogString(string strLog)
        {
            lock (Locker)
            {
                //日志写入哪个盘                
                string sFilePath = System.Windows.Forms.Application.StartupPath + "\\NursDocInfoSysErrorLog\\" + System.DateTime.Now.ToString("yyyyMM");
                string sFileName = "rizhi" + System.DateTime.Now.ToString("ddHHmmss") + ".log";
                sFileName = sFilePath + "\\" + sFileName; //文件的绝对路径
                if (!Directory.Exists(sFilePath))//验证路径是否存在
                {
                    Directory.CreateDirectory(sFilePath);
                    //不存在则创建
                }
                FileStream fs;
                StreamWriter sw;
                if (File.Exists(sFileName))
                //验证文件是否存在，有则追加，无则创建
                {
                    fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write);
                }
                else
                {
                    fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write);
                }
                sw = new StreamWriter(fs);
                sw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "   ---   " + strLog);
                sw.Close();
                fs.Close();
                sw.Dispose();
                fs.Dispose();
            }
        }
        public static void WriteOperatingRecordString(string strLog)
        {
            lock (Locker)
            {
                //日志写入哪个盘
                string sRootFilePath = System.Windows.Forms.Application.StartupPath + "\\NursDocInfoSysOperatingRecord\\";
                string sFilePath = System.Windows.Forms.Application.StartupPath + "\\NursDocInfoSysOperatingRecord\\" + System.DateTime.Now.ToString("yyyyMM");
                string sFileName = "rizhi" + System.DateTime.Now.ToString("MMdd") + ".log";
                sFileName = sFilePath + "\\" + sFileName; //文件的绝对路径
                if (!Directory.Exists(sRootFilePath))//验证路径是否存在
                {
                    Directory.CreateDirectory(sFilePath);
                    FileStream Rootfs;
                    StreamWriter Rootsw;
                    Rootfs = new FileStream(sRootFilePath + "说明.txt", FileMode.Create, FileAccess.Write);
                    Rootsw = new StreamWriter(Rootfs);
                    Rootsw.WriteLine("这是护理路径系统的日志存储文件夹，程序运行时会自动清理上2个月份的日志文件。");
                    Rootsw.WriteLine("例如8月份运行时，6月份的日志将会被删除。");
                    Rootsw.WriteLine("如日志文件庞大，用户也可自行删除。");
                    Rootsw.WriteLine("厦门识凌科技有限公司  2018-08");
                    Rootsw.WriteLine("创建时间：" + System.DateTime.Now.ToString());
                    Rootsw.Close();
                    Rootfs.Close();
                    Rootsw.Dispose();
                    Rootfs.Dispose();
                    //不存在则创建

                }
                if (!Directory.Exists(sFilePath))//验证路径是否存在
                {
                    Directory.CreateDirectory(sFilePath);
                    //不存在则创建
                }
                FileStream fs;
                StreamWriter sw;
                if (File.Exists(sFileName))
                //验证文件是否存在，有则追加，无则创建
                {
                    fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write);
                }
                else
                {
                    fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write);
                }
                sw = new StreamWriter(fs);
                sw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "   ---   " + strLog);
                sw.Close();
                fs.Close();
                sw.Dispose();
                fs.Dispose();
            }
        }
        public static void DeleteOldOperatingRecordString()
        {
            lock (Locker)
            {
                try
                {
                    string directoryPath = System.Windows.Forms.Application.StartupPath + "\\NursDocInfoSysOperatingRecord\\" + System.DateTime.Now.AddMonths(-2).ToString("yyyyMM");
                    if (Directory.Exists(directoryPath))//验证路径是否存在
                    {
                        Directory.GetFiles(directoryPath).ToList().ForEach(File.Delete);
                        Directory.GetDirectories(directoryPath).ToList().ForEach(Directory.Delete);
                        Directory.Delete(directoryPath, true);
                        WriteLog.WriteOperatingRecordString("删除了 " + System.DateTime.Now.AddMonths(-2).ToString("yyyyMM") + "  文件夹中的所有日志 .");
                    }
                    else
                    {
                        WriteLog.WriteOperatingRecordString("没有过期日志 .");
                    }
                }
                catch (IOException e)
                {
                    //  XtraMessageBox.Show(System.DateTime.Now.AddMonths(-2).ToString("yyyyMM") +"中的日志删除失败,请手动删除");
                    WriteLog.WriteOperatingRecordString(System.DateTime.Now.AddMonths(-2).ToString("yyyyMM") + "  文件夹中的日志删除失败 , 返回信息为 : " + e + " .");
                }

            }
        }
      
    }

}
